import type { ApiPagination, ApiPaginationParams } from '#types/utils'
import type { AnyFn } from '@vueuse/core'
import { requestClient } from '../request'
import type { approvalItemForm, CountriesPageList, QrCodeParams } from './model/Common'
// 通用功能
export default class CommonApi {
    static get inst() {
        return CommonApi._inst || (CommonApi._inst = new CommonApi())
    }
    private static _inst: CommonApi
    approval(params: Partial<approvalItemForm>) {
        return requestClient.post<null>('/approvalLog/approval', params)
    }
    // 获取国家列表
    getCountriesPageList(params: ApiPaginationParams<{ name?: string }>) {
        return requestClient.get<ApiPagination<CountriesPageList>>('/countries/index', {
            params
        })
    }

    /**
     * 获取小程序二维码
     * POST /mgCameramanController/uploadCode
     * 接口ID：224147205
     * 接口地址：https://app.apifox.com/link/project/5166966/apis/api-224147205
     */
    getMPCodeUrl(params: QrCodeParams, others: { api: AnyFn; isCache: boolean; sleep?: number }) {
        return requestClient
            .post<string>('/mgCameramanController/uploadCode', params)
            .then(async () => {
                if (!others.isCache) {
                    await CommonApi.inst.sleep(others.sleep || 3000)
                }
                return others.api().then((res: any) => {
                    return res?.qrCode || ''
                })
            })
    }
    /**
     * 服务器时间戳
     * GET /mg/system/timestamp
     * 接口ID：225599428
     * 接口地址：https://app.apifox.com/link/project/5166966/apis/api-225599428
     */
    getServerTime() {
        return requestClient.get<string>('/mg/system/timestamp')
    }
    sleep(ms: number = 1000) {
        return new Promise((resolve) => setTimeout(resolve, ms))
    }
}
